<?php

class ExtSearchController extends Zend_Controller_Action 
{
    protected $group;
    
    public function indexAction()
    {        
        $this->view->form = $this->getForm();
    }
        
    public function itemsAction(){        
        $request = $this->getRequest();
        $start = $request->getParam('start');
        $limit = $request->getParam('limit');
        $query = $request->getParam('query');
        $callback = $request->getParam('callback');
        
        $group = $this->getGroup();
        $data = $group->searchGroups($query, $start, $limit);
        $count = 0;        
        foreach($data as $g){                                                            
            $count++;
        }        
        $final = array();
        $final['totalCount'] =  strval($count);
        $final['groups'] = $data;        
        header('Content-Type', 'application/x-json');
        echo $callback.'('.Zend_Json::encode($final).');';
        die();        
    }
    protected function getGroup()
    {
        if (null === $this->group) {
            require_once APPLICATION_PATH . '/models/Group.php';
            $this->group = new Model_Group();
        }
        return $this->group;
    }
    
    
    private function getForm(){
        require_once APPLICATION_PATH . '/forms/ExtSearch.php';
        $form = new Form_ExtSearch();
        $form->setAction($this->_helper->url('index'));
        return $form;      
    }
     
}
